package co.edu.udea.recursos.hibernate.dao.impl;

import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import co.edu.udea.recursos.hibernate.dao.DAOServidores;
import co.edu.udea.recursos.hibernate.model.Servidores;


/*
 * 
 * @author SANTIAGO RUIZ V - BLADIMIR VELEZ R
 * @version 1.0
 * Clase que implementa los metodos de la interfaz DAOServidores
 * @see DAOCategoria
 */

public class DAOServidoresImpl implements DAOServidores {
	
	private static Logger logger = Logger.getLogger(DAOServidoresImpl.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Servidores> obtenerServidor() {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		List<Servidores> encontradas = new ArrayList<Servidores>();
		
		try {
			encontradas = session.createQuery("from Servidores").list();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return encontradas;
	}

	@Override
	public Servidores obtenerServidor(int codigo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Servidores encontrada = null;
		
		try {
			encontrada = (Servidores) session.get(Servidores.class, codigo);
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return encontrada;
	}

	@Override
	public void guardarServidor(Servidores servidores) {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.save(servidores);
			session.getTransaction().commit();
			
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

	@Override
	public void eliminarServidor(Servidores servidores){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.delete(servidores);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
	}

	@Override
	public void editarServidor(Servidores servidores) {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.merge(servidores);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Servidores> buscarServidor(String cadena){
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		Criteria criteria = session.createCriteria(Servidores.class);
		List<Servidores> encontradas = new ArrayList<Servidores>();
		
		try {
			criteria.add(Restrictions.like("modelo", "%"+cadena+"%"));
			encontradas = criteria.list();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return encontradas;
	}



}
